package com.example.beautyhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.beautyhub.model.Employees;
import com.example.beautyhub.model.Orders;
import com.example.beautyhub.model.Products;
import com.example.beautyhub.service.BeautyProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Products")
@Api(tags = "产品")
public class BeautyProductController {

    @Autowired
    private BeautyProductService beautyProductService;
    @Autowired
    private OrderController orderController;
    @Autowired
    private EmployeeController employeeController;

    @GetMapping("/")
    public List<Products> list() {
        return beautyProductService.list();
    }

    @ApiOperation("搜索")
    @GetMapping("/search")
    public List<Products> getSearchList(@RequestBody Products products) {
        QueryWrapper<Products> wrapper = new QueryWrapper<>();
        wrapper.like("name", products.getName()); //根据条件查询
        return beautyProductService.list(wrapper);//返回分页数据
    }

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Page<Products> getEmployeeList(int pageNum, int pageSize,Products products) {
        Page<Products> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Products> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(products.getName())){
            wrapper.like("name", products.getName()); //根据条件查询
        }
        page.setTotal(beautyProductService.count(wrapper));
        return beautyProductService.page(page, wrapper);//返回分页数据
    }

    @GetMapping("/{id}")
    public Products findById(@PathVariable Integer id) {
        return beautyProductService.getById(id);
    }

    @PostMapping("/")
    public void add(@RequestBody Products products) {
        products.setStockInTime(new Date());
        beautyProductService.save(products);
    }

    @ApiOperation("入库")
    @PutMapping("/inData")
    public void inDate(Integer size, @RequestBody Products products) {
        products.setSize(products.getSize() + size);
        beautyProductService.updateById(products);
    }

    @ApiOperation("出库")
    @PutMapping("/outData")
    public String outDate(Integer size, @RequestBody Products products, Integer customerId, Integer employeeId) {
        if (size > products.getSize()) {
            return "超出库存";
        }
        Orders orders = new Orders();
        orders.setProductId(products.getId());
        orders.setCustomerId(customerId);
        orders.setEmployeeId(employeeId);
        orders.setSize(size);
        orders.setPrice(size * products.getAmount());
        orderController.add(orders);
        products.setSize(products.getSize() - size);

        Employees employees = employeeController.findById(employeeId);
        employees.setSize(employees.getSize() + size);

        products.setStockOutTime(new Date());
        employeeController.update(employeeId, employees);

        beautyProductService.updateById(products);
        return "出库成功";
    }

    @PutMapping("/{id}")
    public void update(@PathVariable Integer id, @RequestBody Products products) {
        products.setId(id);
        beautyProductService.updateById(products);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        beautyProductService.removeById(id);
    }
}

